package cn.nkpro.elcube.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public interface MathUtils {


    static Double irr(List<Double> values) {
        return irr(values, 0.1D);
    }

    static Double irr(List<Double> values, double guess) {
        int maxIterationCount = 20;
        double absoluteAccuracy = 1.0E-007D;

        double x0 = guess;

        int i = 0;
        while (i < maxIterationCount) {
            double fValue = 0.0D;
            double fDerivative = 0.0D;
            for (int k = 0; k < values.size(); k++) {
                fValue += values.get(k) / Math.pow(1.0D + x0, k);
                fDerivative += -k * values.get(k) / Math.pow(1.0D + x0, k + 1);
            }
            double x1 = x0 - fValue / fDerivative;
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                return x1;
            }
            x0 = x1;
            i++;
        }
        return null;
    }
    static Double currencyValue(Double value){
        return BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP).doubleValue();
    }
}
